package demo.main;

import static net.sf.dynamicreports.report.builder.DynamicReports.cht;
import static net.sf.dynamicreports.report.builder.DynamicReports.cmp;
import static net.sf.dynamicreports.report.builder.DynamicReports.field;
import static net.sf.dynamicreports.report.builder.DynamicReports.report;
import static net.sf.dynamicreports.report.builder.DynamicReports.type;

import java.awt.Color;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.dynamicreports.report.builder.chart.ThermometerChartBuilder;
import net.sf.dynamicreports.report.constant.ValueLocation;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRDataSource;

import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;

public class ThermometerChart extends MutiReport {

	@At("/getThermometerChart")
	@Ok("json")
	public String getThermometerChart(HttpServletRequest request,
			HttpServletResponse response) {
		System.out.println("getThermometerChart");
		build(request, response);
		// System.out.println("dddddddd : " + resultResult);
		return resultResult;
	}

	protected void build(HttpServletRequest request,
			HttpServletResponse response) {
		ThermometerChartBuilder chart1 = cht.thermometerChart().setValue(
				field("value", type.integerType()));

		ThermometerChartBuilder chart2 = cht.thermometerChart().setValue(18)
				.setDataRangeHighExpression(30).setValueColor(Color.BLACK)
				.setValueLocation(ValueLocation.BULB)
				.setLowDataRangeLowExpression(0)
				.setLowDataRangeHighExpression(10)
				.setMediumDataRangeLowExpression(10)
				.setMediumDataRangeHighExpression(20)
				.setHighDataRangeLowExpression(20)
				.setHighDataRangeHighExpression(30);

		try {
			reportBuilder = report().setTemplate(Templates.reportTemplate)
					.title(Templates.createTitleComponent(""))
					.summary(cmp.horizontalList(chart1, chart2))
					.pageFooter(Templates.footerComponent)
					.setDataSource(createDataSource());
			setResult(reportBuilder.toJasperPrint(), request, response);
		} catch (DRException e) {
			e.printStackTrace();
		}
	}

	private JRDataSource createDataSource() {
		DataSource dataSource = new DataSource("value");
		dataSource.add(40);
		return dataSource;
	}
}
